<template>
  <div class="user-list">
    <div class="header">
      <h2>用户管理</h2>
      <el-button type="primary" @click="handleAdd">添加用户</el-button>
    </div>

    <el-table :data="userList" border style="width: 100%">
      <el-table-column prop="userId" label="用户ID" width="80" />
      <el-table-column prop="username" label="用户名" width="120" />
      <el-table-column prop="nickname" label="昵称" width="120" />
      <el-table-column prop="email" label="邮箱" width="180" />
      <el-table-column prop="phone" label="手机号" width="120" />
      <el-table-column prop="gender" label="性别" width="80">
        <template #default="scope">
          {{ scope.row.gender === '0' ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="userType" label="用户类型" width="100">
        <template #default="scope">
          {{ getUserTypeText(scope.row.userType) }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="80">
        <template #default="scope">
          <el-tag :type="scope.row.status === '0' ? 'success' : 'danger'">
            {{ scope.row.status === '0' ? '正常' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
        <template #default="scope">
          {{ formatDate(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑用户对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form :model="userForm" label-width="100px">
        <el-form-item label="用户名">
          <el-input v-model="userForm.username" />
        </el-form-item>
        <el-form-item label="密码" v-if="!userForm.userId">
          <el-input v-model="userForm.password" type="password" />
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="userForm.nickname" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="userForm.email" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="userForm.phone" />
        </el-form-item>
        <el-form-item label="性别">
          <el-select v-model="userForm.gender">
            <el-option label="男" value="0" />
            <el-option label="女" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户类型">
          <el-select v-model="userForm.userType">
            <el-option label="管理员" value="01" />
            <el-option label="经销商" value="02" />
            <el-option label="客户" value="03" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="userForm.status">
            <el-option label="正常" value="0" />
            <el-option label="禁用" value="1" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getAllUsers, createUser, updateUser, deleteUser } from '@/api/user'

export default {
  name: 'UserList',
  setup() {
    const userList = ref([])
    const dialogVisible = ref(false)
    const dialogTitle = ref('')
    const userForm = ref({
      username: '',
      password: '',
      nickname: '',
      email: '',
      phone: '',
      gender: '0',
      userType: '03',
      status: '0'
    })

    // 获取用户列表
    const fetchUserList = async () => {
      try {
        const response = await getAllUsers()
        userList.value = response.data
      } catch (error) {
        ElMessage.error('获取用户列表失败')
      }
    }

    // 格式化日期
    const formatDate = (date) => {
      if (!date) return ''
      return new Date(date).toLocaleString()
    }

    // 获取用户类型文本
    const getUserTypeText = (type) => {
      const typeMap = {
        '01': '管理员',
        '02': '经销商',
        '03': '客户'
      }
      return typeMap[type] || '未知'
    }

    // 添加用户
    const handleAdd = () => {
      dialogTitle.value = '添加用户'
      userForm.value = {
        username: '',
        password: '',
        nickname: '',
        email: '',
        phone: '',
        gender: '0',
        userType: '03',
        status: '0'
      }
      dialogVisible.value = true
    }

    // 编辑用户
    const handleEdit = (row) => {
      dialogTitle.value = '编辑用户'
      userForm.value = { ...row }
      dialogVisible.value = true
    }

    // 删除用户
    const handleDelete = (row) => {
      ElMessageBox.confirm('确认删除该用户吗？', '提示', {
        type: 'warning'
      }).then(async () => {
        try {
          await deleteUser(row.userId)
          ElMessage.success('删除成功')
          fetchUserList()
        } catch (error) {
          ElMessage.error('删除失败')
        }
      })
    }

    // 提交表单
    const handleSubmit = async () => {
      try {
        if (userForm.value.userId) {
          await updateUser(userForm.value)
          ElMessage.success('更新成功')
        } else {
          await createUser(userForm.value)
          ElMessage.success('创建成功')
        }
        dialogVisible.value = false
        fetchUserList()
      } catch (error) {
        ElMessage.error(userForm.value.userId ? '更新失败' : '创建失败')
      }
    }

    onMounted(() => {
      fetchUserList()
    })

    return {
      userList,
      dialogVisible,
      dialogTitle,
      userForm,
      handleAdd,
      handleEdit,
      handleDelete,
      handleSubmit,
      formatDate,
      getUserTypeText
    }
  }
}
</script>

<style scoped>
.user-list {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 